import 'package:super_player/super_player.dart';

class VideoDownLoad {
  //单利
  static final VideoDownLoad _instance = VideoDownLoad._internal();

  factory VideoDownLoad() => _instance;

  VideoDownLoad._internal();

  //预加载
  Future<void> startPreLoad(List<String> videoList) async {
    if (videoList.isEmpty) return;

    //设置播放引擎的全局缓存目录和缓存大小
    SuperPlayerPlugin.setGlobalMaxCacheSize(200);
    // 该缓存路径默认设置到app沙盒目录下，postfixPath只需要传递相对缓存目录即可，不需要传递整个绝对路径。
    // Android 平台：视频将会缓存到sdcard的Android/data/your-pkg-name/files/testCache 目录。
    // iOS 平台：视频将会缓存到沙盒的Documents/testCache 目录。
    SuperPlayerPlugin.setGlobalCacheFolderPath("postfixPath");
    String palyrl = "http://****";
  }

  void stopPreLoad(taskId) {
    //取消预下载
    TXVodDownloadController.instance.stopPreLoad(taskId);
  }

  Future<void> download(List<String> videoList) async {
    //启动预下载
    int taskId = await TXVodDownloadController.instance
        .startPreLoad(videoList[0], 1, 1920 * 1080,
            onCompleteListener: (int taskId, String url) {
      print('下载完成，   taskID=${taskId} ,url=${url}');
      videoList.removeAt(0);
      if (videoList.isNotEmpty) {
        download(videoList);
      }
    }, onErrorListener: (int taskId, String url, int code, String msg) {
      print('下载失败  taskID=${taskId} ,url=${url}, code=${code} , msg=${msg}');
      videoList.removeAt(0);
      if (videoList.isNotEmpty) {
        download(videoList);
      }
    });
  }
}
